package org.example.member.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.core.constant.TokenConstants;
import org.example.core.to.LoginMemberTo;
import org.example.core.utils.PageUtils;
import org.example.member.bo.MemberBO;
import org.example.member.constant.CustomConstants;
import org.example.member.dao.MemberDao;
import org.example.member.dto.MemberDto;
import org.example.member.entity.MemberEntity;
import org.example.member.vo.MemberVo;
import org.example.member.vo.front.MemberInfoVo;
import org.example.member.vo.front.MemberUpdateVo;
import org.example.security.handler.SecurityHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.example.member.service.MemberService;

import java.awt.*;
import java.time.LocalDateTime;
import java.util.List;


@Service("memberService")
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    private static final String DEFAULT_PASSWORD = "123456";

    // 登录
    @Override
    public LoginMemberTo login(String phone) {
        LoginMemberTo loginMemberVo = memberDao.getMemberByPhone(phone);
        if (loginMemberVo == null){
            MemberEntity memberEntity = new MemberEntity();
            memberEntity.setPhone(phone);
            memberEntity.setStatus(0);
            String username = CustomConstants.PREFIX_REGISTER + phone;
            memberEntity.setUsername(username);
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String password = passwordEncoder.encode(CustomConstants.DEFAULT_PASSWORD);
            memberEntity.setPassword(password);
            // 注册
            memberEntity.setCreateTime(LocalDateTime.now());
            Long memberId  = memberDao.addMemberInfo(memberEntity);
            LoginMemberTo memberVo = new LoginMemberTo();
            memberVo.setId(memberId);
            memberVo.setStatus(0);
            memberVo.setUsername(username);
            memberVo.setHeader(null);
            return memberVo;
        }
        return loginMemberVo;
    }


    // 获取个人基本信息
    @Override
    public MemberInfoVo queryMemberInfo() {
        String memberId = SecurityHandler.getData().get(TokenConstants.MEMBER_ID);
        return memberDao.getMemberInfo(Long.parseLong(memberId));
    }

    /**
     * 修改用户头像
     * @param header
     * @return
     */
    @Override
    public int updateHeader(String header) {
        String memberId = SecurityHandler.getData().get(TokenConstants.MEMBER_ID);
        return memberDao.updateHeaderMember(header, Long.parseLong(memberId));
    }

    @Override
    public int updateMemberInfo(MemberUpdateVo memberUpdateVo) {
        String memberId = SecurityHandler.getData().get(TokenConstants.MEMBER_ID);
        return memberDao.updateMemberInfo(memberUpdateVo, Long.parseLong(memberId));
    }

    @Override
    public PageUtils list(MemberDto memberDto) {
        if (memberDto.needLimit()){
            PageHelper.startPage(memberDto.getPage(), memberDto.getPageSize());
        }
        List<MemberVo> voList = memberDao.list(memberDto);
        return new PageUtils(new PageInfo<>(voList));
    }

    @Override
    public void refreshPassword(Long id) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String password = bCryptPasswordEncoder.encode(DEFAULT_PASSWORD);
        memberDao.updateMemberPassword(password, id);
    }

    @Override
    public void updateStatus(MemberBO memberBO) {
        memberDao.updateStatus(memberBO);
    }
}